<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容"  v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="orderId"></el-table-column>
        <el-table-column label="订单价格" prop="price"></el-table-column>
        <el-table-column label="是否完成"  width="190">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="已完成"
              inactive-value="未完成"
              active-color="#13ce66"
              inactive-color="#ff0000"
              active-text="已完成"
              inactive-text="未完成"
              @change="statusChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="logisticsStatus" width="190">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.logisticsStatus"
              active-value="已发货"
              inactive-value="未发货"
              active-color="#13ce66"
              inactive-color="#ff0000"
              active-text="已发货"
              inactive-text="未发货"
              @change="logisticsStatusChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="createTime">
          <template slot-scope="scope">{{scope.row.createTime |dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              @click="showOrderDetailsDialog(scope.row)"
            >查看详情</el-button>
            <el-button type="danger"  size="mini" @click="deleteOrder(scope.row.orderId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 展示详情对话框 -->
    <el-dialog title="订单详情" :visible.sync="orderDetailsDialogVisible" width="50%">
      <el-table :data="orderDetails" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名" prop="skuName"></el-table-column>
        <el-table-column label="数量" prop="count"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  data () {
    return {
      // 订单列表查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 订单列表
      orderList: [],
      cityData,
      // 订单详情度对话框
      orderDetailsDialogVisible: false,
      // 订单详情
      orderDetails: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
     if(this.queryInfo.query===''){
        const { data: res } = await this.$http.get(`/xuanmall/order/list`);
        console.log(res);
      if(res.code === 0){
         this.total = res.page.totalCount
         this.orderList = res.page.list
      }
      }
      else{
        const { data: res } = await this.$http.get(`/xuanmall/order/list?page=${this.queryInfo.pageNum}&limit=${this.queryInfo.pageSize}&key=${this.queryInfo.query}`);
        console.log(res);
      if(res.code === 0){
         this.total = res.page.totalCount
         this.orderList = res.page.list
      }
      }
    },
    // 分页
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.pagenum = newSize
      this.getOrderList()
    },
    // 修改订单状态
     async statusChange(data){
       console.log(data)
        const{data:res} = await this.$http.post('/xuanmall/order/update',{
          id:data.orderId,
          status:data.status,
        })
        if(res.code !== 0)return this.$message.error("修改失败")
        this.$message.success("修改成功")
        this.getOrderList()
      },
      async logisticsStatusChange(data){
          const{data:res} = await this.$http.post('/xuanmall/order/update',{
          id:data.orderId,
          logisticsStatus:data.logisticsStatus
        })
        if(res.code !== 0)return this.$message.error("修改失败")
        this.$message.success("修改成功")
        this.getOrderList()
      },
      // 详情弹框显示
    async showOrderDetailsDialog (data) {
      this.orderDetails = data.orderItemReturns
      this.orderDetailsDialogVisible = true
    },
    //删除订单
    async  deleteOrder(id){
       const {data:res} = await this.$http.post('/xuanmall/order/delete',[id])
      if(res.code!==0){ this.$message.error("订单删除失败")}
      this.$message.success("订单删除成功")
      this.getOrderList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
